<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" th:with="title='文章管理',active='article'">
<header th:replace="back/header::headerFragment(${title},${active})"></header>
<head>
    <meta th:name="_csrf" th:content="${_csrf.token}"/>
    <!-- 默认的header name是X-CSRF-TOKEN -->
    <meta th:name="_csrf_header" th:content="${_csrf.headerName}"/>
    <style>
        .article-list-card {
            background: #fff;
            border-radius: 14px;
            box-shadow: 0 4px 24px rgba(60,60,60,0.08);
            padding: 32px 28px 24px 28px;
            margin-top: 24px;
            margin-bottom: 24px;
        }
        .article-list-title {
            display: flex;
            align-items: center;
            font-size: 26px;
            font-weight: 600;
            color: #4a4a4a;
            background: linear-gradient(90deg,#f5f7fa 60%,#e3e6ec 100%);
            border-radius: 12px 12px 0 0;
            box-shadow: 0 2px 8px #e3e6ec;
            padding: 18px 24px 14px 18px;
            margin-bottom: 0;
        }
        .article-list-title i {
            color: #8662c6;
            font-size: 30px;
            margin-right: 16px;
        }
        .table-article {
            border-radius: 10px;
            overflow: hidden;
            box-shadow: 0 2px 8px #e3e6ec;
            margin-bottom: 0;
        }
        .table-article thead th {
            background: #f5f5f5;
            color: #333;
            font-weight: 600;
            border-bottom: 2px solid #e0e0e0;
        }
        .table-article tbody tr:hover {
            background: #f0f4ff;
            transition: background 0.2s;
        }
        .table-article td, .table-article th {
            vertical-align: middle !important;
        }
        .table-article td {
            font-size: 16px;
        }
        .btn {
            border-radius: 8px !important;
            font-size: 15px;
            box-shadow: 0 1px 4px #e3e6ec;
            transition: background 0.2s, box-shadow 0.2s;
        }
        .btn-primary {
            background: linear-gradient(90deg,#8662c6 60%,#a084e8 100%) !important;
            border: none !important;
        }
        .btn-primary:hover {
            background: linear-gradient(90deg,#a084e8 60%,#8662c6 100%) !important;
            box-shadow: 0 4px 16px #b39ddb;
        }
        .btn-danger {
            background: linear-gradient(90deg,#ff5252 60%,#ff867f 100%) !important;
            border: none !important;
        }
        .btn-danger:hover {
            background: linear-gradient(90deg,#ff867f 60%,#ff5252 100%) !important;
            box-shadow: 0 4px 16px #ffb3b3;
        }
        .btn-warning {
            background: linear-gradient(90deg,#ffd740 60%,#ffe57f 100%) !important;
            border: none !important;
            color: #333 !important;
        }
        .btn-warning:hover {
            background: linear-gradient(90deg,#ffe57f 60%,#ffd740 100%) !important;
            box-shadow: 0 4px 16px #fff9c4;
            color: #333 !important;
        }
        .m-b-5 {
            margin-bottom: 5px !important;
        }
        .paging-area {
            margin-top: 30px;
        }
    </style>
</head>
<body class="fixed-left">
<div id="wrapper">
    <div th:replace="back/header::header-body"></div>
    <div class="content-page">
        <div class="content">
            <div class="container">
                <div class="row">
                    <div class="col-sm-12">
                        <div class="article-list-title">
                            <i class="fa fa-list"></i>
                            <span>文章管理</span>
                        </div>
                    </div>
                    <div class="col-md-12">
                        <div class="article-list-card">
                            <table class="table table-striped table-bordered table-article">
                                <thead>
                                <tr>
                                    <th width="35%">文章标题</th>
                                    <th width="15%">发布时间</th>
                                    <th>浏览量</th>
                                    <th>所属分类</th>
                                    <th>操作</th>
                                </tr>
                                </thead>
                                <tbody>
                                <th:block th:each="article : ${articles.list}">
                                    <tr th:id="${article.id}">
                                        <td>
                                            <a th:href="@{'/article/'+${article.id}}" th:text="${article.title}" target="_blank"></a>
                                        </td>
                                        <td><th:block th:text="${commons.dateFormat(article.created)}"/></td>
                                        <td><th:block th:text="${article.hits}"/></td>
                                        <td><th:block th:text="${article.categoryName}"/></td>
                                        <td>
                                            <a th:href="@{'/admin/article/'+${article.id}}"
                                               class="btn btn-primary btn-sm waves-effect waves-light m-b-5">
                                               <i class="fa fa-edit"></i> <span>编辑</span></a>
                                            <a href="javascript:void(0)" th:onclick="'delArticle('+${article.id}+');'"
                                               class="btn btn-danger btn-sm waves-effect waves-light m-b-5">
                                                <i class="fa fa-trash-o"></i> <span>删除</span></a>
                                            <a class="btn btn-warning btn-sm waves-effect waves-light m-b-5 preview-btn" href="javascript:void(0)" th:attr="data-id=${article.id}">
                                                <i class="fa fa-rocket"></i> <span>预览</span></a>
                                        </td>
                                    </tr>
                                </th:block>

                                </tbody>
                            </table>
                            <div class="paging-area" th:replace="comm/paging :: pageAdminNav(${articles})"></div>
                        </div>
                    </div>
                </div>
                <div th:replace="back/footer :: footer-content"></div>
            </div>
        </div>
    </div>
</div>
<div th:replace="back/footer :: footer"></div>
<!-- 文章预览模态框 -->
<style>
#previewModal {
    display: none;
    position: fixed;
    z-index: 9999;
    left: 0; top: 0;
    width: 100vw; height: 100vh;
    background: rgba(0,0,0,0.35);
    align-items: center;
    justify-content: center;
    animation: fadeInBg 0.2s;
}
@keyframes fadeInBg {
    from {background: rgba(0,0,0,0);}
    to {background: rgba(0,0,0,0.35);}
}
#previewModal .preview-dialog {
    background: linear-gradient(135deg,#fff 80%,#f3eaff 100%);
    border-radius: 18px;
    max-width: 700px;
    width: 92vw;
    max-height: 80vh;
    box-shadow: 0 8px 40px 0 rgba(80,40,120,0.18), 0 1.5px 8px 0 rgba(80,40,120,0.10);
    padding: 36px 28px 28px 28px;
    position: relative;
    display: flex;
    flex-direction: column;
    animation: popIn 0.25s cubic-bezier(.68,-0.55,.27,1.55);
}
@keyframes popIn {
    from {transform: scale(0.85); opacity: 0;}
    to {transform: scale(1); opacity: 1;}
}
#closePreviewModal {
    position: absolute;
    top: 14px; right: 22px;
    font-size: 26px;
    background: none;
    border: none;
    color: #8662c6;
    cursor: pointer;
    transition: color 0.2s, background 0.2s;
    border-radius: 50%;
    width: 36px; height: 36px;
    line-height: 36px;
    text-align: center;
}
#closePreviewModal:hover {
    color: #fff;
    background: linear-gradient(135deg,#a084e8 60%,#8662c6 100%);
}
#previewTitle {
    font-size: 2rem;
    font-weight: 700;
    color: #5a3fa0;
    margin-bottom: 18px;
    text-align: center;
    letter-spacing: 1px;
}
#previewContent {
    flex: 1 1 auto;
    overflow-y: auto;
    max-height: 55vh;
    font-size: 1.08rem;
    color: #333;
    padding-right: 2px;
}
@media (max-width: 600px) {
    #previewModal .preview-dialog { padding: 18px 6vw 18px 6vw; }
    #previewTitle { font-size: 1.2rem; }
}
</style>
<div id="previewModal">
    <div class="preview-dialog">
        <button id="closePreviewModal">×</button>
        <h2 id="previewTitle"></h2>
        <div id="previewContent" class="markdown-body"></div>
    </div>
</div>
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
<script>
$(function(){
    $('.preview-btn').click(function(){
        var id = $(this).attr('data-id');
        $.get('/admin/article/preview/' + id, function(data){
            if(data && data.title){
                $('#previewTitle').text(data.title);
                // 使用marked库将markdown转为html
                $('#previewContent').html(marked.parse(data.content||''));
                $('#previewModal').css('display','flex').hide().fadeIn(150);
            }else{
                alert('获取文章失败');
            }
        });
    });
    $('#closePreviewModal').click(function(){
        $('#previewModal').fadeOut(150);
    });
    // 点击模态框外部关闭
    $('#previewModal').click(function(e){
        if(e.target===this){ $(this).fadeOut(150); }
    });
});
</script>
<script type="text/javascript">
    function delArticle(id) {
        // 获取<meta>标签中封装的_csrf信息
        var token = $("meta[name='_csrf']").attr("content");
        var header = $("meta[name='_csrf_header']").attr("content");
        if(confirm('确定删除该文章吗?')){
            $.ajax({
                type:'post',
                url : '/admin/article/delete',
                data: {id:id},
                dataType: 'json',
                beforeSend : function(xhr) {
                    xhr.setRequestHeader(header, token);
                },
                success: function (result) {
                    if (result && result.success) {
                        window.alert("文章删除成功");
                        window.location.reload();
                    } else {
                        window.alert(result.msg || '文章删除失败')
                    }
                }
            });
        }
    }
</script>
</body>
</html>